-- 标题tips

local panelOriginSize = 0 -- panel全显示时候的原始高度
local adjustedHeight = 95 -- equipped不可见时候的显示高度

local disBetweenIconAndTop = 0 -- icon距离上边界距离
local disBetweenNameAndTop = 0 -- name距离上边界距离

local typeTextHeight = 21 -- typeText控件单行文本高度

local disBetweenNameAndCombatTlt = 0    -- name         -- combat_tlt   Y间距
local disBetweenCombatTltAndType = 0    -- combat_tlt   -- type         Y间距
local disBetweenCombatTltAndCombatNum = 0    -- combat_tlt   -- combat_Num         Y间距
local disBetweenTypeAndLv = 0           -- type         -- lv           Y间距

local equippedColor = nil -- equipped控件原始颜色
local lvColor = nil -- type、lv控件原始颜色
local combatTltColor = nil -- combat_tlt控件原始颜色

local newTitleTipsView = class("newTitleTipsView",function()
    return ccui.Layout:create()
end)

function newTitleTipsView:ctor()
    local function createModuleNode(uiPath, uiNodeName, parent)
        local node = cc.uiloader:load( uiPath )
        local moduleNode = node:getChildByName( uiNodeName )
        moduleNode:retain()
        moduleNode:removeFromParent()
        parent:addChild( moduleNode )
        moduleNode:release()
        return moduleNode
    end
    self.mUINode = createModuleNode( Res.Tips_Title, "Tips_1", self )
    panelOriginSize = self.mUINode:getContentSize()
    self:setContentSize( panelOriginSize )

    ccui.Helper:doLayout( self.mUINode )

    local IconRender = require("app.widget.renders.IconRender")
    self._icon = IconRender.create( self.mUINode:getChildByName( "icon" ) ) -- 图标
    self._icon:setClickEnable( false )
    disBetweenIconAndTop = panelOriginSize.height - ( self._icon:getPositionY() + self._icon:getContentSize().height )

    self._name = self.mUINode:getChildByName( "Name" )
    disBetweenNameAndTop = panelOriginSize.height - ( self._name:getPositionY() + self._name:getContentSize().height )

    self._combatTlt = self.mUINode:getChildByName( "combat_tlt" )
    combatTltColor = self._combatTlt:getTextColor()
    self._combatNum = self.mUINode:getChildByName( "combat_num" )
    self._type = self.mUINode:getChildByName( "type" )
    self._lv = self.mUINode:getChildByName( "lv" )
    lvColor = self._lv:getTextColor()

    self._equipped = self.mUINode:getChildByName( "equipped" )
    equippedColor = self._equipped:getTextColor()

    disBetweenNameAndCombatTlt = self._name:getPositionY() - self._combatTlt:getPositionY()
    disBetweenCombatTltAndType = self._combatTlt:getPositionY() - self._type:getPositionY()
    disBetweenCombatTltAndCombatNum = self._combatNum:getPositionY() - self._combatTlt:getPositionY()
    disBetweenTypeAndLv = self._type:getPositionY() - self._lv:getPositionY()
end

function newTitleTipsView:setIconData( iconData )
    self._icon:setData( iconData and iconData or nil )
    self._icon:setClickEnable( false )
end

function newTitleTipsView:setIconKuang(res)
    self._icon:setIconKuang(res)
end

function newTitleTipsView:setNameStr( name )
    self._name:setString( name and name or "" )
    if name and "" ~= name then
        self._name:setVisible( true )
    else
        self._name:setVisible( false )
    end
end
function newTitleTipsView:setNameColor( color )
    if color then
        self._name:setTextColor( color )
    end
end

function newTitleTipsView:setCombatTltStr( strTemp )
    self._combatTlt:setString( strTemp and strTemp or "" )
    if strTemp and "" ~= strTemp then
        self._combatTlt:setVisible( true )
    else
        self._combatTlt:setVisible( false )
    end
end
-- "装备评分"控件改变用途
function newTitleTipsView:changeCombatTltColor( changed )
    if changed then
        self._combatTlt:setTextColor( lvColor )
    else
        self._combatTlt:setTextColor( combatTltColor )
    end
end

function newTitleTipsView:setCombatTltColor(c)
    self._combatTlt:setTextColor( c )
end

function newTitleTipsView:setCombatNumStr( strTemp )
    self._combatNum:setString( strTemp and strTemp or "" )
    if strTemp and "" ~= strTemp then
        self._combatNum:setVisible( true )
    else
        self._combatNum:setVisible( false )
    end
end

function newTitleTipsView:setTypeStr( strTemp )
    self._type:setString( strTemp and strTemp or "" )
    if strTemp and "" ~= strTemp then
        self._type:setVisible( true )
    else
        self._type:setVisible( false )
    end
end

function newTitleTipsView:setTypeColor(c)
    self._type:setTextColor(c)
end

function newTitleTipsView:setLvStr( strTemp )
    self._lv:setString( strTemp and strTemp or "" )
    if strTemp and "" ~= strTemp then
        self._lv:setVisible( true )
    else
        self._lv:setVisible( false )
    end
end
function newTitleTipsView:setLvColor( color )
    if color then
        self._lv:setTextColor( color )
    else
        self._lv:setTextColor( lvColor )
    end
end

function newTitleTipsView:setEquippedStr( strTemp )
    self._equipped:setString( strTemp and strTemp or "" )
    if strTemp and "" ~= strTemp then
        self._equipped:setVisible( true )
    else
        self._equipped:setVisible( false )
    end
end
function newTitleTipsView:setEquippedColor( color )
    if color then
        self._equipped:setTextColor( color )
    else
        self._equipped:setTextColor( equippedColor )
    end
end

-- 内容设置完毕手动调用
function newTitleTipsView:adjustContentSizeAndPos()
    if self._equipped:isVisible() then
        self.mUINode:setContentSize( panelOriginSize )
    else
        if not self._combatTlt:isVisible() or not self._type:isVisible() or not self._lv:isVisible() then -- 这3个控件任一不可见就调整高度
            self.mUINode:setContentSize( cc.size( panelOriginSize.width, adjustedHeight ) )
        else
            self.mUINode:setContentSize( panelOriginSize )
        end
    end
    local mUINodeSizeWidth, mUINodeSizeHeight = self.mUINode:getContentSize2()
    self:setContentSize( cc.size( mUINodeSizeWidth, mUINodeSizeHeight ) )

    -- 调整icon位置
    local _, iconSizeHeight = self._icon:getContentSize2()
    self._icon:setPositionY( mUINodeSizeHeight - disBetweenIconAndTop - iconSizeHeight )
    -- 调整name位置
    local _, nameSizeHeight = self._name:getContentSize2()
    self._name:setPositionY( mUINodeSizeHeight - disBetweenNameAndTop - nameSizeHeight )

    local namePosY = self._name:getPositionY()
    -- 调整 combatTlt
    if self._combatTlt:isVisible() then
        self._combatTlt:setPositionY( namePosY - disBetweenNameAndCombatTlt )
    else
        self._combatTlt:setPositionY( namePosY )
    end

    local combatTltPosY = self._combatTlt:getPositionY()
    self._combatNum:setPositionY( combatTltPosY + disBetweenCombatTltAndCombatNum ) -- 调整combatNum

    -- 调整 type
    local typePosYTemp = 0
    if self._type:isVisible() then
        typePosYTemp = combatTltPosY - disBetweenCombatTltAndType
    else
        typePosYTemp = combatTltPosY
    end
    local typeTextSize = self._type:getVirtualRendererSize()
    if typeTextSize.height > typeTextHeight then
        typePosYTemp = typePosYTemp + typeTextHeight - typeTextSize.height
    end
    self._type:setPositionY( typePosYTemp )

    -- 调整 lv
    local typePosY = self._type:getPositionY()
    if self._lv:isVisible() then
        self._lv:setPositionY( typePosY - disBetweenTypeAndLv )
    else
        self._lv:setPositionY( typePosY )
    end
    ccui.Helper:doLayout( self.mUINode )
end

return newTitleTipsView
